package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.kit.SMSKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.AdminSeller;
import cn.com.dashihui.web.dao.AuthDownload;
import cn.com.dashihui.web.dao.DiXian;
import cn.com.dashihui.web.dao.GbOrder;
import cn.com.dashihui.web.dao.Logistics;
import cn.com.dashihui.web.dao.Order;
import cn.com.dashihui.web.dao.OrderLog;
import cn.com.dashihui.web.dao.OrderLogistics;
import cn.com.dashihui.web.dao.Refound;
import cn.com.dashihui.web.dao.SendSeller;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.dao.User;
import cn.com.dashihui.web.dao.UserValueLog;
import cn.com.dashihui.web.service.AuthDownloadService;
import cn.com.dashihui.web.service.OrderService;
import cn.com.dashihui.web.service.SellerService;
import cn.com.dashihui.web.service.StoreService;
import cn.com.dashihui.web.service.UserService;

@RequiresAuthentication
public class OrderController extends BaseController {
	//使用Duang.duang进行封装，使普通类具有事务的功能
	private OrderService service = Duang.duang(OrderService.class);
	private SellerService sellerService=Duang.duang(SellerService.class);
	private StoreService storeService = new StoreService();
	private UserService userService = new UserService();
	private AuthDownloadService downloadService=new AuthDownloadService();
	
	/**
	 * 兑换验证
	 */
	public void duihuan(){
		render("duihuan.jsp");
	}
	public void doduihuan(){
		int userid=getCurrentUser().getInt("id");
		String storeid=null;
		if(StrKit.notNull(service.findStoreTitle(userid))){
		 storeid=service.findStoreTitle(userid).get("storeid");
		}
		String duihuanma = getPara("duihuanma");
		String gooeitme= service.getOrderByid(duihuanma, storeid);
		if(StrKit.notBlank(gooeitme)){
			Order order = service.getOrderByOrder(duihuanma);
			if(StrKit.notNull(order)){
				renderSuccess();
				return;
			}
		}else{
			renderResult(1);
			return;
		}
		//render("duihuan.jsp");
	}
	/**
	 * 展示验证信息
	 */
	public void dodui(){
		String duihuanma = getPara("duihuanma");
	
		if(StrKit.notBlank(duihuanma)){
			Order order = service.getOrderByOrder(duihuanma);
			setAttr("item", order);
		render("duihuan.jsp");
		}
	}
	
	/**
	 * 进行兑换
	 */
	public void doEatr(){
		String orderNum=getPara("orderNum");
		
		//创建时间
		String deliverDate=DatetimeKit.getFormatDate("yyyy-MM-dd-HH-mm-ss");
		int userid=getCurrentUser().getInt("id");
		String dhstoreid = service.findStoreTitle(userid).get("storeid");
		Order order=service.getOrderByOrderNum(orderNum)
				.set("deliverDate",deliverDate)
				.set("orderState",2)
				.set("dhstoreid", dhstoreid);
		if(service.updateOrder(order)){
			
			Record ordermessage= service.orderMaessage(orderNum);
			String sendNum=ordermessage.get("orderNum");
			String tel=ordermessage.get("tel");
			String goodsName=ordermessage.get("goodsName");
			String name=ordermessage.get("name");
			String title=ordermessage.get("dhstore");
			//买家
			String phone =service.getByPhone(orderNum);
			SMSKit.sendTplVip(phone,title,goodsName,sendNum );
			//平台
			int type=1;
			 Record Ftype=service.FingFtype(orderNum);
			 if(StrKit.notNull(Ftype)){
				 int Ttype=Ftype.get("ftype");
				 if(Ttype==713){
					  type=2;
				   }else{
					  type=3;
				   }
			 }
			 List<Record> record= service.getBySellerPhone(type);
			 for (Record Sellerphone : record) {
				 SMSKit.sendTplSeller(Sellerphone.get("phone"),title,goodsName,sendNum,name,tel);
			 }
			 //公司
			Record records= service.findStoreSellerId(dhstoreid);
			int sellerid=records.getInt("sellerid");
			List<Record> recordse= service.getBySellPhone(sellerid);
			for (Record Sellerphone : recordse) {
				 SMSKit.sendTplSeller(Sellerphone.get("phone"),title,goodsName,sendNum,name,tel);
			 }
			//分公司
			int storeid=records.getInt("id");
			List<Record> recordst= service.getByStorePhone(storeid);
			for (Record Sellerphone : recordst) {
				 SMSKit.sendTplSeller(Sellerphone.get("phone"),title,goodsName,sendNum,name,tel);
			 }
			renderSuccess();
			return;
		}
	}
	
	/**
	 * 订单列表页面
	 */
	public void index(){
		int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
		   setAttr("RoleId",RoleId);
	    if(sellerService.findByUserId(userid)!=null){
			if(RoleId==1||RoleId==2){
		     //setAttr("sellerId", sellerService.findByUserId(userid).get("sellerId"));
				List<Record> list=sellerService.findAllStoreid(userid);
		    	setAttr("list", list);		    
			}
			render("index.jsp");
		}else{
			render("index.jsp");
		}
	}
	/**
	 * 获得订单列表数据
	 */
	public void page(){
		int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		//公司ID
		int seid = 0;
		int stid = 0 ;
		seid = service.findByUserId(userid).get("id");
		//分公司ID
		String storeId = getPara("sStoreId");
		if (StrKit.notBlank(storeId)) {
			Store store = storeService.findByStoreid(storeId);
	 		stid = store.getInt("id");
		}
		renderResult(0,service.findByPage(pageNum,pageSize,orderNum,beginDate,endDate,state,seid,tel,stid,userid));
	}
	 /**
     * 订单统计查询所需积分总和现金价格总和
     */
	public void findTotal(){
		int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		//公司ID
		int seid = 0;
		int stid = 0 ;
		seid = service.findByUserId(userid).get("id");
		//分公司ID
		String storeId = getPara("sStoreId");
		if (StrKit.notBlank(storeId)) {
			Store store = storeService.findByStoreid(storeId);
	 		stid = store.getInt("id");
		}
		renderSuccess(service.findTotal(pageNum,pageSize,orderNum,beginDate,endDate,state,seid,tel,stid,userid));
	}
	/**
     * 订单统计查询所需积分总和现金价格总和
     */
	public void findTotalgb(){
		int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//订单状态
		int isFree = getParaToInt("isFree",0);
		//买家电话
		String tel = getPara("tel");
		//公司ID
		int seid = 0;
		seid = service.findByUserId(userid).get("id");
		//分公司ID
		String storeId = getPara("sStoreId");
		renderSuccess(service.findTotalgb(pageNum,pageSize,orderNum,beginDate,endDate,state,tel,storeId,isFree,seid));
	}
	/**
	 * 团购商品订单列表页面
	 */
	public void groupbuying(){
		List<Record> list=storeService.findAllSeller();
    	setAttr("list", list);
    	List<Record> listStore=storeService.findAllStore();
    	setAttr("listStore", listStore);
		render("groupbuying.jsp");
	}
	/**
	 * 获得团购商品订单列表数据
	 */
	public void pagegb(){
		int userid=getCurrentUser().getInt("id");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		//免单状态  ，因为0被占用9代表全部
		int isFree = getParaToInt("isFree",9);
		//商品id
		int selectGoods = getParaToInt("selectGoods",0);
		//公司ID
		int seid = 0;
		int stid = 0 ;
		seid = service.findByUserId(userid).get("id");
		//分公司ID
		String storeId = getPara("sStoreId");
		if (StrKit.notBlank(storeId)) {
			Store store = storeService.findByStoreid(storeId);
	 		stid = store.getInt("id");
		}
		System.out.println("团购");
		renderResult(0,service.findByPagegb(pageNum,pageSize,orderNum,beginDate,endDate,state,seid,tel,stid,userid,isFree));
	}
	/**
	 * 查询订单详情
	 */
	public void detail(){
		String orderNum = getPara("orderNum");
		Order order = service.findOrderByOrderNum(orderNum);
		if(StrKit.notNull(order)){
			setAttr("obj", order);
			setAttr("goodsList", service.getGoodsListByOrderNum(orderNum));
			setAttr("logList", service.getLogListByOrderNum(orderNum));
			setAttr("eval", service.getEvalByOrderNum(orderNum));
		}
		render("detail.jsp");
	}
	/**
	 * 查询团购订单详情
	 */
	public void detailgb(){
		String orderNum = getPara("orderNum");
		Order order = service.findOrderByOrderNumgb(orderNum);
		if(StrKit.notNull(order)){
			setAttr("obj", order);
			setAttr("goodsList", service.getGoodsListByOrderNumgb(orderNum));
			setAttr("logList", service.getLogListByOrderNum(orderNum));
			setAttr("eval", service.getEvalByOrderNum(orderNum));
		}
		render("detailgb.jsp");
	}
	/**
	 * 会员详情
	 */
	public void userdetail(){
		int id = getParaToInt(0,0);
		if(id!=0){
		User user=service.findById(id);
			setAttr("user", user);
			List<Store> storeList=service.findStoreList();
			setAttr("storeList", storeList);
			List<Record> userList=service.findUserList(user.get("store"));
			setAttr("userList", userList);
		}
		render("userdetail.jsp");
	}
	/**
	 * 填写物流单号
	 */
	/*public void Waybill(){
		String orderNum = getPara("orderNum");
		System.out.println(orderNum+"orderNumorderNumorderNumorderNum");
		render("waybill.jsp");
	}*/
	/**
	 * 去添加物流信息
	 */
	public void toAddWaybill(){
		String orderNum = getPara("orderNum");
		List<Logistics> listLogistics = service.findAllLogistics();
		List<SendSeller> listSendSeller = service.findSendSeller();
		setAttr("listSendSeller", listSendSeller);
		setAttr("listLogistics", listLogistics);
		setAttr("orderNum", orderNum);
		render("addwaybill.jsp");
	}
	/**
	 * 去添加物流信息
	 */
	public void toAddWaybillgb(){
		String orderNum = getPara("orderNum");
		List<Logistics> listLogistics = service.findAllLogistics();
		List<SendSeller> listSendSeller = service.findSendSeller();
		setAttr("listSendSeller", listSendSeller);
		setAttr("listLogistics", listLogistics);
		setAttr("orderNum", orderNum);
		render("addwaybillgb.jsp");
	}
	/**
	 * 去物流信息详情
	 */
	public void WBDetail(){
		String orderNum = getPara("orderNum");
		List<Logistics> listLogistics = service.findAllLogistics();
		List<SendSeller> listSendSeller = service.findSendSeller();
		setAttr("listSendSeller", listSendSeller);
		setAttr("listLogistics", listLogistics);
		setAttr("orderNum", orderNum);
		OrderLogistics orderLogistics = service.findWBByOrderNum(orderNum);
		setAttr("orderLogistics", orderLogistics);
		render("wbdetail.jsp");
	}
	/**
	 * 存储物流和订单信息，预设物流单号——订单为many to one
	 */
	public void doAddWaybillForm(){
		//订单号
		String orderNum = getPara("orderNum");
		//物流单号
		String logisticCode = getPara("logisticCode");
		if (service.findExistsByLogisticCode(logisticCode)) {
			renderResult(1);
			return;
		}
		if (service.findExistsByOrderNum(orderNum)) {
			renderResult(2);
			return;
		}
		//运费
		String orderPay = getPara("orderPay");
		//物流公司代码
		String charset = getPara("charset");
		//发货公司
		String sendSeller = getPara("sendSeller");
		OrderLogistics orderLogistics = new OrderLogistics();
		orderLogistics.set("orderCode", orderNum)
						.set("logisticCode", logisticCode)
						.set("sendSeller", sendSeller)
						.set("orderPay", orderPay)
						.set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd-HH-mm-ss"))
						.set("charset", charset);
		if (orderLogistics.save()) {
			Order order = service.findByOrderNum(orderNum);
			int orderState = 6;
			order.set("orderState", orderState);
			if (order.update()) {
				OrderLog OrderLog = new OrderLog();
				int type = 5;
				int userid = order.get("userid");
				User user = userService.findUserById(userid);
				String name = user.get("name");
				String action = "已出库";
				String content = "已支付订单已出库";
				OrderLog.set("orderNum", orderNum)
								.set("type", type)
								.set("user", name)
								.set("action", action)
								.set("content", content)
								.set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"))
								.save();
				renderSuccess();
			}
		}
	}
	/**
	 * 存储物流和订单信息，预设物流单号——订单为many to one
	 */
	public void doAddWaybillFormgb(){
		//订单号
		String orderNum = getPara("orderNum");
		//物流单号
		String logisticCode = getPara("logisticCode");
		if (service.findExistsByLogisticCode(logisticCode)) {
			renderResult(1);
			return;
		}
		if (service.findExistsByOrderNum(orderNum)) {
			renderResult(2);
			return;
		}
		//运费
		String orderPay = getPara("orderPay");
		//物流公司代码
		String charset = getPara("charset");
		//发货公司
		String sendSeller = getPara("sendSeller");
		OrderLogistics orderLogistics = new OrderLogistics();
		orderLogistics.set("orderCode", orderNum)
						.set("logisticCode", logisticCode)
						.set("sendSeller", sendSeller)
						.set("orderPay", orderPay)
						.set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd-HH-mm-ss"))
						.set("charset", charset);
		if (orderLogistics.save()) {
			GbOrder order = service.findByOrderNumgb(orderNum);
			int orderState = 6;
			order.set("orderState", orderState);
			if (order.update()) {
				OrderLog OrderLog = new OrderLog();
				int type = 5;
				int userid = order.get("userid");
				User user = userService.findUserById(userid);
				String name = user.get("name");
				String action = "已出库";
				String content = "已支付订单已出库";
				OrderLog.set("orderNum", orderNum)
								.set("type", type)
								.set("user", name)
								.set("action", action)
								.set("content", content)
								.set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"))
								.save();
				renderSuccess();
			}
		}
	}
	/**
	 * 拓客详情
	 */
	public void showToker(){
		int tokerid = getParaToInt("tokerid", 0);
		Record goodsToker = service.findToker(tokerid);
		setAttr("goodsToker", goodsToker);
		
		int oneUserid = goodsToker.get("oneUserid");
		if (oneUserid!=0) {
			User oneUser = service.findUser(oneUserid);
			String oneUserName = oneUser.get("name");
			setAttr("oneUserName", oneUserName);
		}
		int twoUserid = goodsToker.get("twoUserid");
		if (twoUserid!=0) {
			User twoUser = service.findUser(twoUserid);
			String twoUserName = twoUser.get("name");
			setAttr("twoUserName", twoUserName);
		}
		int threeUserid = goodsToker.get("threeUserid");
		if (threeUserid!=0) {
			User threeUser = service.findUser(threeUserid);
			String threeUserName = threeUser.get("name");
			setAttr("threeUserName", threeUserName);
		}
		int fourUserid = goodsToker.get("fourUserid");
		if (fourUserid!=0) {
			User fourUser = service.findUser(fourUserid);
			String fourUserName = fourUser.get("name");
			setAttr("fourUserName", fourUserName);
		}
		int fiveUserid = goodsToker.get("fiveUserid");
		if (fiveUserid!=0) {
			User fiveUser = service.findUser(fiveUserid);
			String fiveUserName = fiveUser.get("name");
			setAttr("fiveUserName", fiveUserName);
		}
		
		
		render("showToker.jsp");
	}


	/**
	 * excel导出
	 */
	public void doExcel(){
	  String operationMsg = "excel导出";
	  logger.info(operationMsg);
	  List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("orderNum", "订单号"));
		titles.add(new Pair("name", "会员姓名"));
		titles.add(new Pair("card", "会员卡号"));
		
		titles.add(new Pair("tel", "联系方式"));
	    titles.add(new Pair("xdstore", "下单分公司"));
	    titles.add(new Pair("goodsid", "兑换物品ID"));
	    titles.add(new Pair("goodsName", "兑换物品名称"));
	    titles.add(new Pair("specTitle", "兑换物品规格"));
	    titles.add(new Pair("startDate", "下单时间"));
	    titles.add(new Pair("countnum", "兑换数量"));
	    titles.add(new Pair("pointType", "积分类型"));
	    titles.add(new Pair("payPoint", "所需积分"));
	    titles.add(new Pair("amount", "订单总价值"));
	    titles.add(new Pair("payDate", "支付时间"));
	    titles.add(new Pair("bespokeDate", "预约时间"));
	    titles.add(new Pair("guoDate", "兑换有效期"));
	    titles.add(new Pair("dhstore", "兑换分公司"));
	    titles.add(new Pair("deliverDate", "兑换时间"));
	    titles.add(new Pair("totalPoint", "拓客总积分"));
	    titles.add(new Pair("totalDetion", "拓客总抵扣券"));
	    titles.add(new Pair("totalDcount", "拓客总惠券"));
	    titles.add(new Pair("jjrName", "营业部"));
	    titles.add(new Pair("payState", "状态"));
	    int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
        //订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		String sSellerId =null;
 		String storeId = null;
 		if(RoleId==1||RoleId==2&&StrKit.notNull(service.findByUserId(userid))){
 			 sSellerId =service.findByUserId(userid).get("sellerId");
 			 storeId = getPara("sStoreId");
 		}
 		else if(StrKit.notNull(service.findStoreState(userid))){
 			storeId =service.findStoreState(userid).get("storeid");
 			sSellerId=service.findBysellerId(storeId).get("sellerId");
 		}
		List<Record> list=service.findExcelByPage(orderNum,beginDate,endDate,state,sSellerId,tel,storeId);
		List<Record> listz=new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record=new Record();
			record.set("tel", list.get(i).getStr("tel"));
			record.set("orderNum",list.get(i).get("orderNum"));
			record.set("name",list.get(i).get("name"));
			//record.set("card",list.get(i).get("card"));
			record.set("xdstore", list.get(i).get("xdstore"));
			record.set("goodsid", list.get(i).get("goodsid"));
			record.set("goodsName", list.get(i).get("goodsName"));
			record.set("specTitle", list.get(i).get("specTitle"));
			record.set("startDate", list.get(i).get("startDate"));
			record.set("countnum", list.get(i).get("countnum"));
//			if(list.get(i).get("pointType").equals(1)){
//				record.set("pointType", "积分");
//			}else if(list.get(i).get("pointType").equals(2)){
//				record.set("pointType", "抵扣券");
//			}else if(list.get(i).get("pointType").equals(3)){
//				record.set("pointType", "惠券");
//			}
			record.set("payPoint", list.get(i).get("payPoint"));
			record.set("amount", list.get(i).get("amount"));
			record.set("payDate", list.get(i).get("payDate"));
			record.set("bespokeDate", list.get(i).get("bespokeDate"));
			record.set("guoDate", list.get(i).get("guoDate"));
			record.set("dhstore", list.get(i).get("dhstore"));
			record.set("deliverDate", list.get(i).get("deliverDate"));
			if(list.get(i).get("payState").equals(1)){
				record.set("payState", "未支付");
			}else if(list.get(i).get("payState").equals(2)){
				if(list.get(i).get("orderState").equals(1)){
					if(list.get(i).get("tuistate").equals(0)){
						record.set("payState", "未兑换");
					}else if(list.get(i).get("tuistate").equals(1)){
						record.set("payState", "退款中");
					}else if(list.get(i).get("tuistate").equals(2)){
						record.set("payState", "已退款");
					}
					
				}else if(list.get(i).get("orderState").equals(2)){
					record.set("payState", "已付款");
				}else if(list.get(i).get("orderState").equals(3)){
					
					if(list.get(i).get("tuistate").equals(0)){
						record.set("payState", "已过期");
					}else if(list.get(i).get("tuistate").equals(1)){
						record.set("payState", "退款中");
					}else if(list.get(i).get("tuistate").equals(2)){
						record.set("payState", "已退款");
					}
				}else if(list.get(i).get("orderState").equals(4)){
					record.set("payState", "已删除");
				}
			}
				record.set("card", list.get(i).get("card"));
				record.set("totalPoint", list.get(i).get("totalPoint"));
				record.set("totalDetion", list.get(i).get("totalDetion"));
				record.set("totalDcount", list.get(i).get("totalDcount"));
				record.set("jjrName", list.get(i).get("jjrName"));
			listz.add(record);
 		}
		System.out.println("ahaskldhaksdkl=========="+getCurrentUser().getInt("id"));
		//向登录下载统计表中添加数据 订单统计
	    Record downObj=downloadService.findByUserId1(getCurrentUser().getInt("id"));
		System.out.println("downObj+"+downObj);
	    AuthDownload download=new AuthDownload();
		download.set("logName","公司端")
			        .set("name", downObj.get("trueName"))
			        .set("accountRole", downObj.get("alias"))
			        .set("storeName", downObj.get("title"))
			        .set("orderDownload", 1)
                   .set("downloadDate",DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));

		download.save();
		// 导出
		service.export(getResponse(), getRequest(),titles, listz);		
		logger.info( "成功");
		renderNull();
	}
	/**
	 * 团购订单excel导出
	 */
	public void doExcelgb(){
	  String operationMsg = "团购订单excel导出";
	  logger.info(operationMsg);
	  List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("orderNum", "订单号"));
		titles.add(new Pair("name", "买家姓名"));
		titles.add(new Pair("tel", "联系方式"));
	    titles.add(new Pair("xdstore", "下单分公司"));
	    titles.add(new Pair("goodsid", "兑换物品ID"));
	    titles.add(new Pair("goodsName", "兑换物品名称"));
	    titles.add(new Pair("specTitle", "兑换物品规格"));
	    titles.add(new Pair("startDate", "下单时间"));
	    titles.add(new Pair("countnum", "兑换数量"));
	    titles.add(new Pair("pointType", "积分类型"));
	    titles.add(new Pair("payPoint", "所需积分"));
	    titles.add(new Pair("amount", "现金价格"));
	    titles.add(new Pair("payDate", "支付时间"));
	    titles.add(new Pair("bespokeDate", "预约时间"));
	    titles.add(new Pair("guoDate", "兑换有效期"));
	    titles.add(new Pair("dhstore", "兑换分公司"));
	    titles.add(new Pair("deliverDate", "兑换时间"));
	    titles.add(new Pair("payState", "状态"));
	    int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
        //订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		String sSellerId =null;
 		String storeId = null;
 		if(RoleId==1||RoleId==2&&StrKit.notNull(service.findByUserId(userid))){
 			 sSellerId =service.findByUserId(userid).get("sellerId");
 			 storeId = getPara("sStoreId");
 		}
 		else if(StrKit.notNull(service.findStoreState(userid))){
 			storeId =service.findStoreState(userid).get("storeid");
 			sSellerId=service.findBysellerId(storeId).get("sellerId");
 		}
		List<Record> list=service.findExcelByPagegb(orderNum,beginDate,endDate,state,sSellerId,tel,storeId);
		List<Record> listz=new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record=new Record();
			record.set("tel", list.get(i).getStr("tel"));
			record.set("orderNum",list.get(i).get("orderNum"));
			record.set("name",list.get(i).get("name"));
			//record.set("card",list.get(i).get("card"));
			record.set("xdstore", list.get(i).get("xdstore"));
			record.set("goodsid", list.get(i).get("goodsid"));
			record.set("goodsName", list.get(i).get("goodsName"));
			record.set("specTitle", list.get(i).get("specTitle"));
			record.set("startDate", list.get(i).get("startDate"));
			record.set("countnum", list.get(i).get("countnum"));
			if(list.get(i).get("pointType").equals(1)){
				record.set("pointType", "积分");
			}else if(list.get(i).get("pointType").equals(2)){
				record.set("pointType", "抵扣券");
			}else if(list.get(i).get("pointType").equals(3)){
				record.set("pointType", "惠券");
			}
			record.set("payPoint", list.get(i).get("payPoint"));
			record.set("amount", list.get(i).get("amount"));
			record.set("payDate", list.get(i).get("payDate"));
			record.set("bespokeDate", list.get(i).get("bespokeDate"));
			record.set("guoDate", list.get(i).get("guoDate"));
			record.set("dhstore", list.get(i).get("dhstore"));
			record.set("deliverDate", list.get(i).get("deliverDate"));
			if(list.get(i).get("payState").equals(1)){
				record.set("payState", "未支付");
			}else if(list.get(i).get("payState").equals(2)){
				if(list.get(i).get("orderState").equals(1)){
					if(list.get(i).get("tuistate").equals(0)){
						record.set("payState", "未兑换");
					}else if(list.get(i).get("tuistate").equals(1)){
						record.set("payState", "退款中");
					}else if(list.get(i).get("tuistate").equals(2)){
						record.set("payState", "已退款");
					}
					
				}else if(list.get(i).get("orderState").equals(2)){
					record.set("payState", "已付款");
				}else if(list.get(i).get("orderState").equals(3)){
					
					if(list.get(i).get("tuistate").equals(0)){
						record.set("payState", "已过期");
					}else if(list.get(i).get("tuistate").equals(1)){
						record.set("payState", "退款中");
					}else if(list.get(i).get("tuistate").equals(2)){
						record.set("payState", "已退款");
					}
				}else if(list.get(i).get("orderState").equals(4)){
					record.set("payState", "已删除");
				}
			}
				//record.set("createDate", list.get(i).get("createDate"));
			listz.add(record);
 		}
		System.out.println("ahaskldhaksdkl=========="+getCurrentUser().getInt("id"));
		//向登录下载统计表中添加数据 订单统计
	    Record downObj=downloadService.findByUserId1(getCurrentUser().getInt("id"));
		System.out.println("downObj+"+downObj);
	    AuthDownload download=new AuthDownload();
		download.set("logName","公司端")
			        .set("name", downObj.get("trueName"))
			        .set("accountRole", downObj.get("alias"))
			        .set("storeName", downObj.get("title"))
			        .set("orderDownload", 1)
                   .set("downloadDate",DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));

		download.save();
		// 导出
		service.exportgb(getResponse(), getRequest(),titles, listz);		
		logger.info( "成功");
		renderNull();
	}
	/**
	 * 积分兑换
	 */
	public void dixian(){
		int userid=getCurrentUser().getInt("id");
		Record record=service.findStore(userid);
		logger.info("===>"+record);
		setAttr("record", record);
		render("dixian.jsp");
	}
	/**
	 * 手机查询用户
	 */
	public void phone(){
		String phone = getPara("phone");
		Record record=service.findUserByPhone(phone);
		renderSuccess(record);
		return;
	}
	/**
	 * 保存积分兑换订单
	 */
	public void savedixian(){
		String orderNum="DL"+DatetimeKit.getFormatDate("yyyyMMddHHmmssSSS");
		String phone = getPara("phone");//会员手机号
		String name = getPara("name");//会员姓名
		String mun = getPara("mun");//消费项目
		//String money = getPara("money");//总
		int amount = getParaToInt("amount");//折扣
		String amounts = getPara("amounts");//实际
		String storeid = getPara("storeid");
		int userid=getCurrentUser().getInt("id");
		String storeTitle = service.findStoreTitle(userid).get("title");
		String pointName="";
		int type=getParaToInt("type");
		int sppoint=0;
		int jkpoint=0;
		int jypoint=0;
		if(type==1){
			sppoint=amount;
			pointName="积分";
		}else if(type==2){
			jkpoint=amount;
			pointName="抵扣券";
		}else if(type==3){
			jypoint=amount;
			pointName="惠券";
		}
		DiXian dixian=new DiXian();
		dixian.set("phone", phone)
		.set("storeid", storeid)
		.set("mun", mun)
		.set("money", amounts)
		.set("amount", amount)
		.set("date", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"))
		.set("orderNum", orderNum);
		if(dixian.save()){
			User user=service.findByUser(phone);
			if(type==1){
				user.set("sppoint", user.getInt("sppoint")-amount).update();
			}else if(type==2){
				user.set("jkpoint", user.getInt("jkpoint")-amount).update();
			}else if(type==3){
				user.set("jypoint", user.getInt("jypoint")-amount).update();
			}
			UserValueLog userValue=new UserValueLog();
			userValue.set("userid", user.getInt("id"))
			.set("type", 2)
			.set("describe", "积分抵现")
			.set("storeid", storeid)
			.set("goodsName", mun)
			.set("sppoint", sppoint)
			.set("dedu", jkpoint)
			.set("coupon", jypoint)
			.set("data", 1)
			.set("fromOrderNum", orderNum).set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"))
			.save();
			//平台
			int stype=1;
			/* Record Ftype=service.FingFtype(orderNum);
			 if(StrKit.notNull(Ftype)){
				 int Ttype=Ftype.get("ftype");
				 if(Ttype==713){
					  stype=2;
				   }else{
					  stype=3;
				   }
			 }*/
			 List<Record> record= service.getBySellerPhone(stype);
			 for (Record Sellerphone : record) {
				 SMSKit.sendTplSellers(Sellerphone.get("phone"),phone,name,storeTitle,mun,pointName,amount);
			 }
			 //公司
			Record records= service.findStoreSellerId(storeid);
			int sellerid=records.getInt("sellerid");
			List<Record> recordse= service.getBySellPhone(sellerid);
			for (Record Sellerphone : recordse) {
				 SMSKit.sendTplSellers(Sellerphone.get("phone"),phone,name,storeTitle,mun,pointName,amount);
			 }
			//分公司
			int storeId=records.getInt("id");
			List<Record> recordst= service.getByStorePhone(storeId);
			for (Record Sellerphone : recordst) {
				 SMSKit.sendTplSellers(Sellerphone.get("phone"),phone,name,storeTitle,mun,pointName,amount);
			 }
			
			renderSuccess();
		}else{
			renderFailed();
		}
	}
	/**
	 * 积分兑换列表
	 */
	public void dixianlist(){
		int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
		   setAttr("RoleId",RoleId);
	    if(sellerService.findByUserId(userid)!=null){
			if(RoleId==1||RoleId==2){
				List<Record> list=sellerService.findAllStoreid(userid);
		    	setAttr("list", list);
			}
			render("dixianlist.jsp");
		}else{
			render("dixianlist.jsp");
		}
	}
	/**
	 * 获得抵现列表数据
	 */
	public void dixianpage(){
		int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String toBossOneId = getPara("toBossOneId");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//买家电话
		String phone = getPara("phone");
		String sSellerId =null;
 		String storeId = null;
 		if(RoleId==1||RoleId==2&&StrKit.notNull(service.findByUserId(userid))){
 			 sSellerId =service.findByUserId(userid).get("sellerId");
 			storeId = getPara("sStoreId");
 		}else if(StrKit.notNull(service.findStoreState(userid))){
 			storeId =service.findStoreState(userid).get("storeid");
 			sSellerId=service.findBysellerId(storeId).get("sellerId");
 		}
		renderResult(0,service.findBydixianPage(pageNum,pageSize,toBossOneId,beginDate,endDate,sSellerId,phone,storeId));
	}
	/**
	 * excel导出
	 */
	public void doDixianExcel(){
	  String operationMsg = "excel导出";
	  logger.info(operationMsg);
	  List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("title", "消费分公司"));
		titles.add(new Pair("name", "会员姓名"));
		titles.add(new Pair("phone", "联系方式"));
	    titles.add(new Pair("store", "开卡分公司"));
	    titles.add(new Pair("toBossOneId", "销售"));
	    titles.add(new Pair("mun", "消费项目"));
	    titles.add(new Pair("money", "消费金额"));
	    titles.add(new Pair("amount", "抵现金额"));
	    titles.add(new Pair("date", "消费时间"));
	    int userid=getCurrentUser().getInt("id");
		int RoleId= sellerService.findByRoleId(userid);
        //订单编号
		String toBossOneId = getPara("toBossOneId");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//买家电话
		String phone = getPara("phone");
		String sSellerId =null;
 		String storeId = null;
 		if(RoleId==1||RoleId==2&&StrKit.notNull(service.findByUserId(userid))){
 			 sSellerId =service.findByUserId(userid).get("sellerId");
 			 storeId = getPara("sStoreId");
 		}
 		else if(StrKit.notNull(service.findStoreState(userid))){
 			storeId =service.findStoreState(userid).get("storeid");
 			sSellerId=service.findBysellerId(storeId).get("sellerId");
 		}
		List<Record> list=service.findExcelByPage(toBossOneId,beginDate,endDate,sSellerId,phone,storeId);
		List<Record> listz=new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record=new Record();
			record.set("title", list.get(i).getStr("title"));
			record.set("name",list.get(i).get("name"));
			record.set("phone",list.get(i).get("phone"));
			record.set("store", list.get(i).get("store"));
			record.set("toBossOneId", list.get(i).get("toBossOneId"));
			record.set("mun", list.get(i).get("mun"));
			record.set("money", list.get(i).get("money"));
			record.set("amount", list.get(i).get("amount"));
			record.set("date", list.get(i).get("date"));
			listz.add(record);
 		}
		// 导出
		service.exportdi(getResponse(), getRequest(),titles, listz);		
		logger.info( "成功");
		renderNull();
	}
	public void convert(){
		int userId=getParaToInt("userId", 0);	
		setAttr("userId", userId);
		render("index.jsp");
	}

	/**
	 * 展示此会员的所有线上订单
	 */
	public void doConvert(){
		int userId=getParaToInt("userId", 0);	
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0,service.findUserOrder(pageNum,pageSize,userId));	
	}
	  /***
     *  导出此会员的所有线上订单
     */
	public void doConvertExcel(){
		int userId=getParaToInt("userId", 0);	
		List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("orderNum", "订单号"));
		titles.add(new Pair("card", "会员卡号"));
		titles.add(new Pair("name", "会员姓名"));
		titles.add(new Pair("tel", "联系方式"));
	    titles.add(new Pair("xdstore", "下单分公司"));
	    titles.add(new Pair("goodsid", "兑换物品ID"));
	    titles.add(new Pair("goodsName", "兑换物品名称"));
	    titles.add(new Pair("specTitle", "兑换物品规格"));
	    titles.add(new Pair("startDate", "下单时间"));
	    titles.add(new Pair("countnum", "兑换数量"));
	    titles.add(new Pair("pointType", "积分类型"));
	    titles.add(new Pair("payPoint", "所需积分"));
	    titles.add(new Pair("amount", "现金价格"));
	    titles.add(new Pair("payDate", "支付时间"));
	    titles.add(new Pair("bespokeDate", "预约时间"));
	    titles.add(new Pair("guoDate", "兑换有效期"));
	    titles.add(new Pair("dhstore", "兑换分公司"));
	    titles.add(new Pair("deliverDate", "兑换时间"));
	    titles.add(new Pair("toBossOneId", "营业部"));
	    titles.add(new Pair("payState", "状态"));
/*		     //订单编号
	  		String orderNum = getPara("orderNum");
	  		//下单时间范围
	  		String beginDate = getPara("beginDate");
	  		String endDate = getPara("endDate");
	  		//订单状态
	  		int state = getParaToInt("state",0);
	  		//买家电话
	  		String tel = getPara("tel");
	  		//公司ID
	  		String sellerId=getPara("sellerId");
	  		//分公司ID
	  		String storeId=getPara("storeId");*/
		List<Record> list=service.findUserOrder(userId);
		List<Record> listz=new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record=new Record();
			record.set("tel", list.get(i).getStr("tel"));
				record.set("orderNum",list.get(i).get("orderNum"));
				record.set("card",list.get(i).get("card"));
				record.set("name",list.get(i).get("name"));
				record.set("xdstore", list.get(i).get("xdstore"));
				record.set("goodsid", list.get(i).get("goodsid"));
				record.set("goodsName", list.get(i).get("goodsName"));
				record.set("specTitle", list.get(i).get("specTitle"));
				record.set("startDate", list.get(i).get("startDate"));
				record.set("countnum", list.get(i).get("countnum"));
				if(list.get(i).get("pointType").equals(1)){
					record.set("pointType", "积分");
				}else if(list.get(i).get("pointType").equals(2)){
					record.set("pointType", "抵扣券");
				}else if(list.get(i).get("pointType").equals(3)){
					record.set("pointType", "惠券");
				}
				record.set("payPoint", list.get(i).get("payPoint"));
				record.set("amount", list.get(i).get("amount"));
				record.set("payDate", list.get(i).get("payDate"));
				record.set("bespokeDate", list.get(i).get("bespokeDate"));
				record.set("guoDate", list.get(i).get("guoDate"));
				record.set("dhstore", list.get(i).get("dhstore"));
				record.set("deliverDate", list.get(i).get("deliverDate"));
				record.set("toBossOneId", list.get(i).get("toBossOneId"));
				if(list.get(i).get("payState").equals(1)){
					if(list.get(i).get("delState").equals(0)){
						record.set("payState", "待付款");
					}else if(list.get(i).get("delState").equals(1)){
						record.set("payState", "已取消");
					}
				}else if(list.get(i).get("payState").equals(2)){
				   if(list.get(i).get("orderState").equals(1)){
						if(list.get(i).get("tuistate").equals(0)){
							record.set("payState", "未兑换");
						}else if(list.get(i).get("tuistate").equals(1)){
							record.set("payState", "退款中");
						}else if(list.get(i).get("tuistate").equals(2)){
							record.set("payState", "已退款");
						}
						
					}else if(list.get(i).get("orderState").equals(2)){
						record.set("payState", "已付款");
					}else if(list.get(i).get("orderState").equals(3)){
						
						if(list.get(i).get("tuistate").equals(0)){
							record.set("payState", "已过期");
						}else if(list.get(i).get("tuistate").equals(1)){
							record.set("payState", "退款中");
						}else if(list.get(i).get("tuistate").equals(2)){
							record.set("payState", "已退款");
						}
					}else if(list.get(i).get("orderState").equals(4)){
						record.set("payState", "已删除");
					}
				}
			listz.add(record);
 		}
		//向登录下载统计表中添加数据   订单统计
	    AuthDownload downObj=downloadService.findByUserId(getCurrentUser().getInt("id"));
		AuthDownload download=new AuthDownload();
		download.set("logName","平台端")
			        .set("name", downObj.get("trueName"))
			        .set("accountRole", downObj.get("alias"))
			        .set("storeName", "公司总部")
			        .set("orderDownload", 1)
                   .set("downloadDate",DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));

		download.save();
 
		// 导出
		service.export(getResponse(), getRequest(),titles, listz);		
		renderNull();
	}
	//去驳回退款申请
	public void toRefound(){
		String orderNum = getPara("orderNum");
		System.out.println("tiaozhuan ");
		setAttr("orderNum", orderNum);
		render("doCancel.jsp");
	}
	//驳回退款申请
		public void doRefound(){
			String orderNum = getPara("orderNum");
			String refuseReason = getPara("refuseReason");
			Order order = service.findByOrderNum(orderNum);
			Integer orderUserId = order.getInt("userid");
			User orderUser = service.findUser(orderUserId);
			Double oldSppoint = orderUser.getDouble("sppoint");
			Double oldJypoint = orderUser.getDouble("jypoint");
			Double oldJkpoint = orderUser.getDouble("jkpoint");
			Double sppoint = order.getDouble("sppoint");
			Double jypoint = order.getDouble("jypoint");
			Double jkpoint = order.getDouble("jkpoint");
			if (order.getInt("tuistate")!=1) {
				renderResult(1);
				return;
			}
			int tuistate = 3;
			order.set("tuistate", tuistate);
			if (order.update()) {
				Refound refound = service.findRefound(orderNum);
				if (refound!=null) {
					int state = 3;
					refound.set("state", state);
					refound.set("refuseReason", refuseReason);
					if (refound.update()) {
						OrderLog OrderLog = new OrderLog();
						int type = 14;
						int AdId=getCurrentUser().getInt("id");
						AdminSeller adseller = service.findAdseller(AdId);
						String trueName = adseller.get("trueName");
						String action = "审核";
						String content = "驳回审核";
						OrderLog.set("orderNum", orderNum)
										.set("type", type)
										.set("user", trueName)
										.set("action", action)
										.set("content", content)
										.set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"))
										;
						if (OrderLog.save()) {
							orderUser.set("sppoint", sppoint+oldSppoint)
									.set("jypoint", jypoint+oldJypoint)
									.set("jkpoint", jkpoint+oldJkpoint)
									;
						}
						if (orderUser.update()) {
							UserValueLog userValue=new UserValueLog();
							userValue.set("userid", adseller.getInt("id"))
							.set("type", 1)
							.set("flag", 19)
							.set("fromOrderNum", orderNum)
							.set("describe", "驳回退款申请")
							.set("sppoint", sppoint)
							.set("dedu", jkpoint)
							.set("coupon", jypoint)
							.set("data", 1)
							.set("fromOrderNum", orderNum).set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"))
							.save();
						}
					}
				}
				renderSuccess(service.findByOrderNum(orderNum));
			}else{
				renderFailed("此订单未申请退款");
			}
		}
	//通过退款申请
	public void doAccept(){
		String orderNum = getPara("orderNum");
		Order order = service.findByOrderNum(orderNum);
		if (order.getInt("tuistate")!=1) {
			renderResult(1);
			return;
		}
		int tuistate = 2;
		order.set("tuistate", tuistate);
		if (order.update()) {
			Refound refound = service.findRefound(orderNum);
			int state = 2;
			refound.set("state", state);
			if (refound.update()) {
				OrderLog OrderLog = new OrderLog();
				int type = 14;
				int userid=getCurrentUser().getInt("id");
				User user = userService.findUserById(userid);
				String name = user.get("name");
				String action = "审核";
				String content = "通过审核";
				OrderLog.set("orderNum", orderNum)
								.set("type", type)
								.set("user", name)
								.set("action", action)
								.set("content", content)
								.set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"))
								.save();
				renderSuccess(service.findByOrderNum(orderNum));
				return ;
			}
		}
	}

}
